<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Admin_comments extends Admin_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('comments_mdl');
	}
	
	public function index()
	{
		
	}

	 /**
     * 管理留言和引用
     * 
     * @access public
     * @param  string $status 状态
     * @return void
     */
	public function comment_list($pid = '', $status = 'approved')
	{
		$keywords = $this->input->post('keywords');
		$page = max(1, intval($this->input->post('pageNum')));
		$limit = 18; //每页显示数
		$offset = ($page - 1) * $limit;


		//分页
		$data['totalCount'] = 40000;
		$data['numPerPage'] = $limit;
		$data['currentPage'] = $page;
		$data['keywords'] = $keywords;
		if (!empty($keywords))
		{
			$data['comment_list'] = $this->comments_mdl->get_cmts($pid, '', $status, $limit, $offset, 'DESC', $keywords)->result_array();
		}
		else
		{
			$data['comment_list'] = $this->comments_mdl->get_cmts($pid, '', $status, $limit, $offset, 'DESC', $keywords)->result_array();
		}

		$this->load->view('comment', $data);
		
		
		/** 查看日志下的评论 */
		$pid = $this->input->get('pid',TRUE);
		if(!empty($pid))
		{
			$post = $this->posts_mdl->get_post_by_id('pid', $pid);
			
			if(empty($post))
			{
				show_error('ERROR!');
				exit();
			}
			
			$this->_data['page_title'] = '文章：'. $post->title .' 的评论';
			
			unset($post);
			
			$query[] = 'pid='.$pid;	
		}
		

		
		
		
		// if($comments)
		// {
		// 	foreach($comments->result() as $comment)
		// 	{
		// 		$comment->post = $this->posts_mdl->get_post_by_id('pid', $comment->pid);
		// 	}
		// }
	
		// $this->_data['comments'] = $comments;
		// $this->_data['status'] = $status;
		
	}

	 /**
     * 操作留言和引用
     * 
     * @access public
     * @return void
     */
	public function operate()
	{
		/** 尝试get获取数据 */
		$action = $this->input->get('do',TRUE);
		
		/* 不是get传递来的数据? */
		if(empty($action))
		{
			/** 换post试试 */
			$action = $this->input->post('do',TRUE);
		}
		
		switch($action)
		{
			case 'get':
				$this->_get();
				break;
			case 'edit':
				$this->_edit();
				break;
			case 'spam':
				$this->_spam();
				break;
			case 'approved';
				$this->_approved();
				break;
			case 'waiting':
				$this->_waiting();
				break;
			case 'delete':
				$this->_delete();
				break;
			case 'delete-spam':
				$this->_delete_spam();
				break;
			default:
				show_404();
				break;
		}
	}
	
	 /**
     * 获得一条评论内容（ajax）
     * 
     * @access private
     * @return void
     */
	private function _get()
	{
		$cid = $this->input->get('cid',TRUE);
		$comment = $this->comments_mdl->get_cmt($cid);
		
		if($comment && $this->_is_cmt_writable($comment))
		{
			throwJson(array(
                'success'   => 1,
                'comment'   => $comment
            ));
		}
		else
		{
			throwJson(array(
                'success'   => 0,
                'comment'   => '获取评论失败'
            ));
		}
	}

	 /**
     * 编辑
     * 
     * @access private
     * @return void
     */
	private function _edit()
	{
		$cid = $this->input->get('cid',TRUE);
		
		//to do: 添加验证配置
		
		$comment = $this->comments_mdl->get_cmt($cid);
		
		if($comment && $this->_is_cmt_writable($comment))
		{
			$data = array(
				'text' => trim($this->input->post('text',TRUE)),
				'author' => trim($this->input->post('author',TRUE)),
				'mail' => trim($this->input->post('mail',TRUE)),
				'url' => trim($this->input->post('url',TRUE))
			);
		
			$this->comments_mdl->update_cmt($cid, $data);
			
			$updated = $this->comments_mdl->get_cmt($cid);
			
			throwJson(array(
                'success'   => 1,
                'comment'   => $updated
            ));
		}

		throwJson(array(
            'success'   => 0,
            'comment'   => '修改评论失败'
        ));
	}

	 /**
     * 是否可写
     * 
     * @access private
     * @param  object $comment 评论对象
     * @return bool
     */
	private function _is_cmt_writable($comment)
	{
		if($this->auth->exceed('editor',TRUE))
		{
			return TRUE;
		}
		
		if($comment->ownerid == $this->user->uid)
		{
			return TRUE;
		}
		
		return FALSE;
	}

	 /**
     * 处理垃圾评论
     * 
     * @access private
     * @return void
     */
	private function _spam()
	{
		$comments = $this->_fetch_cid_as_array();
		$affected_rows = 0;
		
		if(!empty($comments))
		{
			foreach($comments as $comment)
			{
				if($this->mark($comment, 'spam'))
				{
					$affected_rows ++;
				}
			}	
		}
		
		($affected_rows >0)
				?$this->_jui_message('操作成功')
				:$this->_jui_message('操作失败', 'error');
	}

	 /**
     * 处理待审核评论
     * 
     * @access private
     * @return void
     */
	private function _waiting()
	{
		$comments = $this->_fetch_cid_as_array();
		$affected_rows = 0;
		
		if(!empty($comments))
		{
			foreach($comments as $comment)
			{
				if($this->mark($comment,'waiting'))
				{
					$affected_rows ++;
				}
			}	
		}
		
		($affected_rows >0)
				?$this->_jui_message('操作成功')
				:$this->_jui_message('操作成功', 'error');
	}

	 /**
     * 处理正常通过评论
     * 
     * @access private
     * @return void
     */
	private function _approved()
	{
		$comments = $this->_fetch_cid_as_array();
		$affected_rows = 0;
		
		if(!empty($comments))
		{
			foreach($comments as $comment)
			{
				if($this->mark($comment,'approved'))
				{
					$affected_rows ++;
				}
			}	
		}
		
		($affected_rows >0)
				?$this->_jui_message('操作成功')
				:$this->_jui_message('操作失败', 'error');
	}

	 /**
     * 删除评论
     * 
     * @access private
     * @return void
     */
	private function _delete()
	{
		$comments = $this->_fetch_cid_as_array();
		$deleted = 0;
		
		if(!empty($comments))
		{
			foreach($comments as $comment)
			{
				$comment = $this->comments_mdl->get_cmt($comment);
				
				/** 删除评论 */
				$this->comments_mdl->remove_cmt(array('cid' => $comment->cid));
				
				$deleted ++;
			}
		}

		($deleted >0)
				?$this->_jui_message('操作成功')
				:$this->_jui_message('操作失败', 'error');;
	
	}
	
	 /**
     * 删除垃圾评论
     * 
     * @access private
     * @return void
     */
	private function _delete_spam()
	{
		$condition = array('status' => 'spam');
		
		if('on' !== $this->session->userdata('__all_comments') && !$this->auth->exceed('editor',TRUE))
		{
			array_push($condition, array('ownerid' => $this->user->uid));
		}
		
		if($this->input->get('pid',TRUE))
		{
			array_push($condition, array('pid' => $this->input->get('pid',TRUE)));
		}
		
		$this->comments_mdl->remove_cmt($condition);
		
		$this->_jui_message('操作成功');
	}

    /**
     * 标记评论状态
     * 
     * @access private
     * @param integer $cid 评论主键
     * @param string $status 状态
     * @return boolean
     */
	public function mark($cid, $status)
	{
		$comment = $this->comments_mdl->get_cmt($cid);
		
		if($comment && $this->_is_cmt_writable($comment))
		{
			/** 不必更新的情况 */
			if($status == $comment->status)
			{
				return FALSE;
			}
			
			/** 更新评论 */
			$this->comments_mdl->update_cmt($cid, array('status' => $status));
			
			/** 更新相关内容的评论数 */
			if ('approved' == $comment->status && 'approved' != $status) 
			{
				$this->posts_mdl->cmts_num_minus($comment->pid);
            } 
            else if ('approved' != $comment->status && 'approved' == $status) 
            {
                $this->posts_mdl->cmts_num_plus($comment->pid);
            }
            
            return TRUE;
		}
		
		return FALSE;
	}

	 /**
     * 获取待操作的评论ID数组
     * 
     * @access private
     * @return array
     */
	private function _fetch_cid_as_array()
	{
		/** 尝试get获取数据 */
		$cid = $this->input->get('cid',TRUE);
		
		/* 不是get传递来的数据? */
		if(empty($cid))
		{
			/** 换post试试 */
			$cid = $this->input->post('cid',TRUE);
		}
		
		return $cid ? (is_array($cid) ? $cid : array($cid)) : array();
	}
}

/* End of file Comments.php */
/* Location: ./application/controllers/admin/Comments.php */
